package com.yuting.server;

import com.yuting.Exception.StudentNumException;
import com.yuting.FileTools.StoreAndReadFromFile;
import com.yuting.model.Student;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

/**
 * @author cyt
 * @date 2020/7/2
 */
public class StudentManagement implements ManageModule<Student> {
    //所有的学生信息
    private List<Student> students;

    //学生信息存储和读取的位置
    private final static String STUDENT_INFO = "D:\\students.txt";


    /**
     * add a student into studentInfo
     * @param student
     * @return
     */
    @Override
    public boolean add(Student student) {
        //遍历整个List查看是否已经存在该学生
        for(Student s : students){

            if(s.getStudentNum() == student.getStudentNum()){
//                System.out.println("该学生已存在，请选择修改或删除功能！");
                try {
                    throw new StudentNumException("该学号已经存在，请选择修改或删除功能！");
                } catch (StudentNumException e) {
                    e.printStackTrace();
                    return false;
                }

            }
        }

        return students.add(student);
    }


    /**
     * delete a student according to his or her studentNum
     * @param student
     * @return
     */
    @Override
    public boolean delete(Student student) {
        ListIterator listIterator = students.listIterator();
        while (listIterator.hasNext()){
            Student temp = (Student) listIterator.next();
            if(temp.getStudentNum() == student.getStudentNum()){
                listIterator.remove();
            }
        }
        return true;
    }

    /**
     * modify the information of a student
     * @param student
     * @return
     */
    @Override
    public boolean modify(Student student) {
        //先查看是否存在此学生
        int count = 0;
        for(int i = 0; i <= students.size() - 1; i++){
            if(students.get(i).getStudentNum() == student.getStudentNum()){
                students.get(i).setName(student.getName());
                students.get(i).setAge(student.getAge());
                count++;
            }
        }
        if(count == 0){
            try {
                throw new StudentNumException("该学号不存在，请先添加该学生！");
            } catch (StudentNumException e) {
                e.printStackTrace();
                return false;
            }
        }

        return true;
    }

    /**
     * 根据学生的学号搜索该学生的信息
     * @param o
     * @return
     */
    @Override
    public Student search(Student o) {
        for(Student s : students){
            if(s.getStudentNum() == o.getStudentNum()){
                return s;
            }
        }
        try {
            throw new StudentNumException("该学号不存在，请先创建该学生！");
        } catch (StudentNumException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * store student infomation into a file
     */
    @Override
    public void storeData() {
        StoreAndReadFromFile.InfoStoring(students,STUDENT_INFO);
    }

    /**
     * read the student information from a file
     */
    @Override
    public void readData() {

        this.students = (List<Student>)StoreAndReadFromFile.readInfo(STUDENT_INFO);
        if(this.students == null){
            this.students = new ArrayList<>();
        }
        System.out.println(this.students);
    }

    //校验学生账户的登录
    public boolean studentLogin(int account, String pwd){
        for(Student s : students){
            if(s.getStudentNum() == account && s.getPassword().equals(pwd)){
                return true;
            }
        }
        return false;
    }

    /**
     * 给学生打分
     * @param studentNum
     * @param score
     */
    public void assess(int studentNum, int score){
        for(Student s : students){
            if(s.getStudentNum() == studentNum){
                s.setScore(score);
            }
        }
    }



}
